﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;

namespace ExcelJsonParser
{
    class JsonViewWorkSheets
    {
        List<JsonViewWorkSheet> _jsonViewWorkSheetList = new List<JsonViewWorkSheet>();
        public JsonViewWorkSheets()
        {
        }

        public JsonViewWorkSheet InitJsonView(Range jsonSource)
        {
            JsonViewWorkSheet jsonVWs = null;
            if (TryGetByName(jsonSource, out jsonVWs) == false)
            {
                jsonVWs = JsonViewWorkSheet.NewJsonViewWorkSheet(JsonWorkApp.GetInstance().GetJsonViewWorkBook(), jsonSource);
                _jsonViewWorkSheetList.Add(jsonVWs);
            }
            else
            {
                jsonVWs.UpdateJsonViewWorkSheet(jsonSource);
            }
            return jsonVWs;
        }

        public Boolean TryGetByName(Range jsonSource, out JsonViewWorkSheet jsonViewWorkSheet)
        {
            foreach (JsonViewWorkSheet jsonVWs in _jsonViewWorkSheetList)
            {

                if (jsonSource.Equals(jsonVWs.JsonSource))
                {
                    jsonViewWorkSheet = jsonVWs;
                    return true;
                }
            }
            jsonViewWorkSheet = null;
            return false;
        }

        public Boolean TryGetByWorksheet(Worksheet ws, out JsonViewWorkSheet jsonViewWorkSheet)
        {
            foreach (JsonViewWorkSheet jsonVWs in _jsonViewWorkSheetList)
            {

                if (ws.Equals(jsonVWs.WorkSheet))
                {
                    jsonViewWorkSheet = jsonVWs;
                    return true;
                }
            }
            jsonViewWorkSheet = null;
            return false;
        }
    }
}
